package com.jilian.powerstation.modul.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.google.gson.Gson;
import com.jilian.powerstation.MyApplication;
import com.jilian.powerstation.R;
import com.jilian.powerstation.base.BaseActivity;
import com.jilian.powerstation.common.dto.PcsInfoDto;
import com.jilian.powerstation.common.dto.PowerDto;
import com.jilian.powerstation.listener.CustomItemClickListener;
import com.jilian.powerstation.manege.Weather.ForecastInfo;
import com.jilian.powerstation.manege.Weather.WeaterManager;
import com.jilian.powerstation.manege.Weather.WeatherInfo;
import com.jilian.powerstation.modul.adapter.TemDateListAdapter;
import com.jilian.powerstation.modul.adapter.TemperatureListAdapter;
import com.jilian.powerstation.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import interfaces.heweather.com.interfacesmodule.bean.Lang;
import interfaces.heweather.com.interfacesmodule.bean.Unit;
import interfaces.heweather.com.interfacesmodule.bean.search.Search;
import interfaces.heweather.com.interfacesmodule.bean.weather.forecast.Forecast;
import interfaces.heweather.com.interfacesmodule.bean.weather.forecast.ForecastBase;
import interfaces.heweather.com.interfacesmodule.bean.weather.hourly.Hourly;
import interfaces.heweather.com.interfacesmodule.bean.weather.hourly.HourlyBase;
import interfaces.heweather.com.interfacesmodule.bean.weather.now.Now;
import interfaces.heweather.com.interfacesmodule.view.HeWeather;

public class WeatherActivity extends BaseActivity implements CustomItemClickListener, BDLocationListener {
    private TextView tvCurrTemperature;
    private TextView tvWeatherInfo1;
    private TextView tvCurrStatus;
    private TextView tvTitle1;
    private RecyclerView rvCurrWeater;
    private RecyclerView mRecycle;
    private PowerDto mPowerDto;

    private TemperatureListAdapter adapter;
    private List<HourlyBase> mDatas;
    private TemDateListAdapter mAdapter;
    private List<ForecastInfo.ListBean> mListDatas;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void createViewModel() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_weather;
    }

    @Override
    public void initView() {
        tvCurrTemperature = (TextView) findViewById(R.id.tv_curr_temperature);
        tvWeatherInfo1 = (TextView) findViewById(R.id.tv_weather_info1);
        tvCurrStatus = (TextView) findViewById(R.id.tv_curr_status);
        tvTitle1 = (TextView) findViewById(R.id.tv_title1);
        rvCurrWeater = (RecyclerView) findViewById(R.id.rv_curr_weater);
        mRecycle = (RecyclerView) findViewById(R.id.rv_date_weater);

        setTranslTitle("Weather forecast", v -> finish());
        iniRecycle();
    }

    @Override
    public void initData() {
        mPowerDto = (PowerDto) getIntent().getSerializableExtra("data");
        StatusBarUtil.setStatusBarMode(this, false, R.color.color_3E3B38);
        startLocationCilent();
        getWeater(mPowerDto.getLatitude(),mPowerDto.getLongitude());
    }

    @Override
    public void initListener() {
    }

    private void iniRecycle() {
        mDatas = new ArrayList<>();
        adapter = new TemperatureListAdapter(mDatas, this, this);
        rvCurrWeater.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rvCurrWeater.setAdapter(adapter);

        mListDatas = new ArrayList<>();
        mAdapter = new TemDateListAdapter(mListDatas, this, this);
        mRecycle.setLayoutManager(new LinearLayoutManager(this));
        mRecycle.setAdapter(mAdapter);
    }

    /**
     * 开启定位
     */
    private void startLocationCilent() {
        // 申请权限
        MyApplication.getInstance().mLocationClient.start();
        MyApplication.getInstance().mLocationClient.registerLocationListener(this);
    }

    @Override
    public void onItemClick(View view, int position) {

    }

    private double currentLat;//当前位置的维度
    private double currentLon;//当前位置的维度

    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        if (bdLocation != null) {
            currentLat = bdLocation.getLatitude();
            currentLon = bdLocation.getLongitude();
            Log.e("TAG_WEATHER", "Exception------>" +bdLocation.getCity()+ new Gson().toJson(bdLocation));
            MyApplication.getInstance().mLocationClient.stop();
        }
    }

    WeaterManager weaterManager = new WeaterManager();
    private Lang lang = Lang.ENGLISH;
    private Unit unit = Unit.METRIC;

    private void getWeater(double currentLat, double currentLon) {
        weaterManager.getWeather("https://api.openweathermap.org/data/2.5/weather", currentLat, currentLon, new WeaterManager.HttpCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                WeatherActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!TextUtils.isEmpty(o)) {
                            WeatherInfo info = null;
                            try {
                                info = new Gson().fromJson(o, WeatherInfo.class);
                                Log.e("TAG_WEATHER", "Exception------>" + new Gson().toJson(info));
                            } catch (Exception e) {
                                return;
                            }
                            tvCurrTemperature.setText((int)info.getMain().getTemp() + "°C");
//                            tvWeatherInfo1.setText(info.getMain().getTemp_min() + "～" + info.getMain().getTemp_max() + "°C");
                            if (info.getWeather() != null && info.getWeather().size() > 0) {
                                WeatherInfo.WeatherBean weatherBean = info.getWeather().get(0);
                                tvCurrStatus.setText(weatherBean.getMain());
                            }
                        }
                    }
                });

            }

            @Override
            public void onError(Exception e) {

            }
        });
        weaterManager.getWeather("https://api.openweathermap.org/data/2.5/forecast", currentLat, currentLon, new WeaterManager.HttpCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                WeatherActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!TextUtils.isEmpty(o)) {
                            ForecastInfo info = null;
                            try {
                                info = new Gson().fromJson(o, ForecastInfo.class);
                            } catch (Exception e) {
                                return;
                            }
                            mListDatas.clear();
                            mListDatas.addAll(info.getList());
                            mAdapter.notifyDataSetChanged();
                            tvWeatherInfo1.setText(info.getCity().getName());
                        }
                    }
                });

            }

            @Override
            public void onError(Exception e) {
                Log.e("TAG_WEATHER", "Exception------>" + new Gson().toJson(e));
            }
        });
    }

}
